#include "main.h"
#include "system.h"
UART_HandleTypeDef LogUart;
UART_HandleTypeDef CommunicationUart;
UART_HandleTypeDef NBUart;

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if (huart->Instance == Log_Uart)
  {
    
    __HAL_RCC_USART6_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /***USART6 GPIO Configuration    
    	PC0     ------> USART6_TX
    	PC1     ------> USART6_RX 
    	***/
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF2_USART6;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = RS485_DE_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(RS485_DE_PORT, &GPIO_InitStruct);
	RS485_TX_Data;
	

    
    HAL_NVIC_SetPriority(USART3_6_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART3_6_IRQn);
  }
  else if (huart->Instance == NB_Uart)
  {
    
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    
    HAL_NVIC_SetPriority(USART1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  }
  else if (huart->Instance == Communication_Uart)
  {
    
    __HAL_RCC_USART3_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_USART3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
    HAL_NVIC_SetPriority(USART3_6_IRQn, 2, 0);
    HAL_NVIC_EnableIRQ(USART3_6_IRQn);
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
  if (huart->Instance == Log_Uart)
  {

    
    __HAL_RCC_USART2_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_1);
    
    HAL_NVIC_DisableIRQ(USART3_6_IRQn);
  }
  else if (huart->Instance == NB_Uart)
  {

    
    __HAL_RCC_USART1_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
    
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  }
  else if (huart->Instance == Communication_Uart)
  {

    
    __HAL_RCC_USART3_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4 | GPIO_PIN_5);
    
    HAL_NVIC_DisableIRQ(USART3_6_IRQn);
  }
}

void Print_UART_Init(void)
{
  LogUart.Instance = USART6;
  LogUart.Init.BaudRate = 115200;
  LogUart.Init.WordLength = UART_WORDLENGTH_8B;
  LogUart.Init.StopBits = UART_STOPBITS_1;
  LogUart.Init.Parity = UART_PARITY_NONE;
  LogUart.Init.Mode = UART_MODE_TX_RX;
  LogUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  LogUart.Init.OverSampling = UART_OVERSAMPLING_16;
  LogUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  LogUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&LogUart) != HAL_OK)
  {
    Error_Handler();
  }
}

uint8_t	LCD_Uart_Buf[10]={0};  

void Commu_UART_Init(void)
{
  CommunicationUart.Instance = USART3;
  CommunicationUart.Init.BaudRate = 38400;
  CommunicationUart.Init.WordLength = UART_WORDLENGTH_8B;
  CommunicationUart.Init.StopBits = UART_STOPBITS_1;
  CommunicationUart.Init.Parity = UART_PARITY_NONE;
  CommunicationUart.Init.Mode = UART_MODE_TX_RX;
  CommunicationUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  CommunicationUart.Init.OverSampling = UART_OVERSAMPLING_16;
  CommunicationUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  CommunicationUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&CommunicationUart) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_UART_Receive_IT(&CommunicationUart,(uint8_t *)LCD_Uart_Buf,1);
}

uint8_t	NB_Uart_Buf[10]={0};  

void NBLORA_UART_Init(void)
{
  NBUart.Instance = USART1;
  NBUart.Init.BaudRate = 9600;
  NBUart.Init.WordLength = UART_WORDLENGTH_8B;
  NBUart.Init.StopBits = UART_STOPBITS_1;
  NBUart.Init.Parity = UART_PARITY_NONE;
  NBUart.Init.Mode = UART_MODE_TX_RX;
  NBUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  NBUart.Init.OverSampling = UART_OVERSAMPLING_16;
  NBUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  NBUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  NBUart.gState=HAL_UART_STATE_RESET;
  if (HAL_UART_Init(&NBUart) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_UART_Receive_IT(&NBUart,(uint8_t *)NB_Uart_Buf,1);
}


#if 1

uint8 Uart_NBReceDataBuffer[sizeof(ReceDataInfor)]={0};
ReceDataInfor*gNB_ReceData=(ReceDataInfor*)Uart_NBReceDataBuffer;

uint8 Uart_LCDReceDataBuffer[sizeof(ReceDataInfor)]={0};
ReceDataInfor*gLCD_ReceData=(ReceDataInfor*)Uart_LCDReceDataBuffer;


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  uint8 temp = 0;
  if (huart->Instance == NB_Uart)
  {
  	
	gNB_ReceData->buf[gNB_ReceData->len++]=NB_Uart_Buf[0];
	if(gNB_ReceData->len>=Uart_ReceBufLen)
	{
		gNB_ReceData->len=0;
	}  
  }
  else if (huart->Instance == Communication_Uart)
  {
	
	 if(gLCD_ReceData->processed==1)
	 {
		gLCD_ReceData->buf[gLCD_ReceData->len++]=LCD_Uart_Buf[0];
		if(gLCD_ReceData->len>=Uart_ReceBufLen)
		{
			gLCD_ReceData->len=0;
		}  	 

	 }

  }
}

void  USART3_6_IRQHandler(void)
{
	HAL_UART_IRQHandler(&CommunicationUart);
	HAL_UART_Receive_IT(&CommunicationUart,(uint8_t *)LCD_Uart_Buf,1);
}
#endif

void	USART1_IRQHandler(void)
{
	HAL_UART_IRQHandler(&NBUart);
	HAL_UART_Receive_IT(&NBUart,(uint8_t *)NB_Uart_Buf,1);

}

int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&LogUart, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}

void	SendDataToNBModule(uint8	*buf,uint8	len)
{
	 	
		HAL_UART_Transmit(&NBUart, (uint8_t *)buf, len, 0xffff);
}



s32 RtspGetStringContent(s8 *buf, s8 *starttitle,s8 *endtitle, s8 *content)
{
    s8 *start = NULL;
    s8 *end = NULL;
    s32 len = 0;
    if (buf == NULL || starttitle == NULL || content == NULL) {
        printf("check args null");
        return -1;
    }

    start = strstr((c8*)buf, (c8*)starttitle);
    if (start == NULL) {
        printf("can't find title:%s", starttitle);
        return -1;
    }

    start += strlen(starttitle);
    while (*start == ' ') {
        start++;
    }

    
    end = strstr(start,endtitle);
    if (end == NULL) {
        
        return -1;
    }

    len = end - start;
    strncpy(content, start, len);
    content[len] ='\0';
    return len;
}

u32 UtilGetIntFlag(u8 *buf,u8*startflag,u8*endflag)
{
    s8  tmpBuf[128];
    s32 ret;
    memset(tmpBuf, 0, sizeof(tmpBuf));
    ret = RtspGetStringContent(buf, startflag,endflag, tmpBuf);
    if (ret > 0) {
        return (u32)atol(tmpBuf);
    }
    return 0;
}

uint8	UtilGetStringFlag(u8 *buf,u8*startflag,u8*endflag,u8*content)
{
	memset(content, 0, sizeof(content));
    if (RtspGetStringContent(buf,startflag,endflag,content) > 0)
	{
        return TRUE;
    }

    return FALSE;
} 

uint16 StringToHex(uint8 *sourse_str,uint8 len,uint8*destination_str)
{
	uint8 hightbyte,lowbyte,i=0;

	
	for(i=0;i<len;i=i+2)	
	{
		if((sourse_str[i]<'a')&&(sourse_str[i]>'f')||(sourse_str[i]<'0')&&(sourse_str[i]>'9'))
		{
			printf("--->>str err\n");
			return	FALSE;
		}
		if (sourse_str[i]>= 'a')
		{
			hightbyte=(sourse_str[i] - 'a' + 10) * 16;
		}
		else
		{
			hightbyte = (sourse_str[i] - '0') * 16;
		}
		if (sourse_str[i+1] >= 'a')
		{
			lowbyte = sourse_str[i+1] - 'a' + 10;
		}
		else
		{
			lowbyte = sourse_str[i+1] - '0';
		}
		destination_str[i/2] =hightbyte + lowbyte;
	}

	
	return	TRUE;
}
IWDG_HandleTypeDef	hiwdg;

void WatchdogInitial(void)
{
	
	hiwdg.Instance = IWDG;
	hiwdg.Init.Prescaler = IWDG_PRESCALER_128;
	hiwdg.Init.Window = 4095;
	hiwdg.Init.Reload = 4095;
	HAL_IWDG_Init(&hiwdg);
	
}

void FeedWatchDog(void)
{
	HAL_IWDG_Refresh(&hiwdg);
}
